 package cparc;

import com.mysql.jdbc.Statement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;




/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pesqAnimal.java
 *
 * Created on 14/01/2010, 19:49:24
 */

/**
 *
 * @author danilo
 */
public class pesqAnimal extends javax.swing.JInternalFrame {

    static cadastroAnim frameAnimal;


    public void setAnimal(cadastroAnim fAnim){
       frameAnimal = fAnim;
    }

    /** Creates new form pesqAnimal */
    public pesqAnimal() {
        initComponents();

        tfConsulta.requestFocusInWindow();
        tfConsulta.selectAll();

        DefaultTableModel modelo = (DefaultTableModel) tbAnimal.getModel();
        modelo.setRowCount(0);
        Statement stm = null;
        String consulta = "SELECT ACOD, ANOM, APRO FROM CPARC.CAD_ANIM ORDER BY ACOD ASC";
        int i=0;
        try  {
            stm = (Statement) conexao.getConnection(true).createStatement();
            ResultSet rs = stm.executeQuery(consulta);

            while(rs.next()){
                modelo.addRow(new String[] {Integer.toString(rs.getInt("ACOD")), rs.getString("ANOM"),Integer.toString(rs.getInt("APRO"))});
                modelo.fireTableCellUpdated(i, 0);
                modelo.fireTableCellUpdated(i, 1);
                modelo.fireTableCellUpdated(i, 2);
                i++;
            }

        } catch(Exception e){
            e.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tbAnimal = new javax.swing.JTable();
        btConsulta = new javax.swing.JButton();
        tfConsulta = new javax.swing.JTextField();

        setTitle("Pesquisa de Animais");

        tbAnimal.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Nome do Animal", "Código do Proprietário"
            }
        ));
        tbAnimal.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbAnimalKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tbAnimal);

        btConsulta.setText("Consultar");
        btConsulta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConsultaActionPerformed(evt);
            }
        });

        tfConsulta.setText("Digite o código do animal");
        tfConsulta.setToolTipText("Pesquisa de Animais");
        tfConsulta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfConsultaKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tfConsulta, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(btConsulta))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btConsulta))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(69, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tbAnimalKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbAnimalKeyPressed

        if(evt.getKeyCode() == 10 ){

            this.dispose();
            frameAnimal.requestFocusInWindow();
            frameAnimal.animal((String) tbAnimal.getValueAt(tbAnimal.getSelectedRow(), 0), (String) tbAnimal.getValueAt(tbAnimal.getSelectedRow(), 1),(String) tbAnimal.getValueAt(tbAnimal.getSelectedRow(), 2));
            frameAnimal.setAcod((String) tbAnimal.getValueAt(tbAnimal.getSelectedRow(), 0));
        }
}//GEN-LAST:event_tbAnimalKeyPressed

    private void btConsultaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConsultaActionPerformed

        DefaultTableModel modelo = (DefaultTableModel) tbAnimal.getModel();
        modelo.setRowCount(0);
        Statement stm = null;
        String acod = tfConsulta.getText();
        String consulta = null;
        //if(acod.equals("Digite o código do animal")){
            // JOptionPane.showMessageDialog(rootPane, "Por favor Digite o Código do animal!", "Alerta!", WIDTH);
           // tfConsulta.requestFocusInWindow();
           // tfConsulta.selectAll();
       //}
        if(!acod.isEmpty()){
                consulta = "SELECT ACOD, ANOM, APRO FROM CPARC.CAD_ANIM WHERE ACOD = "+ acod;
        }else if (acod.isEmpty() || acod.equals("Digite o código do animal") ){
             consulta = "SELECT ACOD, ANOM, APRO FROM CPARC.CAD_ANIM ORDER BY ACOD ASC";
        }
         System.out.println(consulta);
        int i=0;
        try  {
            stm = (Statement) conexao.getConnection(true).createStatement();
            ResultSet rs = stm.executeQuery(consulta);

            while(rs.next()){
                modelo.addRow(new String[] {Integer.toString(rs.getInt("ACOD")), rs.getString("ANOM"),Integer.toString(rs.getInt("APRO"))});
                modelo.fireTableCellUpdated(i, 0);
                modelo.fireTableCellUpdated(i, 1);
                modelo.fireTableCellUpdated(i, 2);
                i++;
            }

        } catch(Exception e){
            e.printStackTrace();
        }
        tbAnimal.requestFocusInWindow();
}//GEN-LAST:event_btConsultaActionPerformed

    private void tfConsultaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfConsultaKeyPressed

        if(evt.getKeyCode() == 10){

            btConsulta.doClick();
        }

        else if(evt.getKeyCode() == 27){

                this.dispose();
                frameAnimal.requestFocusInWindow();
                frameAnimal.escAnimal();
            }
}//GEN-LAST:event_tfConsultaKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private static javax.swing.JButton btConsulta;
    private javax.swing.JScrollPane jScrollPane1;
    private static javax.swing.JTable tbAnimal;
    public javax.swing.JTextField tfConsulta;
    // End of variables declaration//GEN-END:variables

}
